#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_logminer.py 
@version:
@time: 2022/05/17 
"""
import time

import allure

from TapClientConnector import TapClientConnector
from TapClientCore.T06_Tapdata_feature.v2.TPF01_datasource.tpf_datasource_v2 import V2TPFDataSource
from TapClientCore.T06_Tapdata_feature.v2.TPF07_logcollector.tpf_logcollector import TPFLogCollector
from TapClientCore.T07_CaseTemplate.v2.CT03_task.ct_task import CTTask
from TapUtil.logfunc.logger import logger


class CTLogMiner(V2TPFDataSource, TPFLogCollector, CTTask):
    # 日志挖掘任务状态检查
    def check_logminer_status(self, connection_name, task_name):
        time.sleep(30)
        with allure.step('数据源日志挖掘任务存在'):
            self.wait_logcollector_create(connection_name)
        with allure.step('数据源日志挖掘状态检查'):
            self.logcollector_status_running(connection_name)
        # 这个对应的就是任务状态
        # with allure.step('数据源日志挖掘子任务状态检查'):
        #     self.logminer_status_running(connection_name, task_name)

    @staticmethod
    def init_connector(db_info):
        return TapClientConnector(db_info)

    def log_miner_reset(self, connection_name):
        self.logcollector_status_ready(connection_name)
        # 任务启动后会自动开始任务，不用那个启动共享挖掘
        # self.start_logcollector_by_name(connection_name)

    def LOG_MINER_TEMPLATE_01(self, parallel_data):
        data = None
        source_delete = None
        record_id = None
        _source = None
        _target = None
        task_name = None
        try:
            self.random_wait()
            # 并行案例初始化
            with allure.step('获取可用表'):
                data = self.lock_and_get_table(parallel_data)
            record_id = data.get('id')
            # operate info
            operate = data.pop('operate')
            operate_source = operate.get('source')
            operate_target = operate.get('target')
            source_info = operate_source.get('source_info')
            target_info = operate_target.get('target_info')
            # connector
            _source = self.init_connector(source_info)
            _target = self.init_connector(target_info)
            # ddl and dml
            source_table = operate_source.get('table')
            source_insert_column = operate_source.get('insert')
            source_update_column = operate_source.get('update')
            source_where_column = operate_source.get('where')
            target_table = operate_target.get('table')

            data['task_config']['config']['name'] += source_table
            task_name = data.get('task_config').get('config').get('name')

            _source.tapclient_connector.CURRENT_TABLE = source_table
            _target.tapclient_connector.CURRENT_TABLE = target_table
            source_insert = _source.tapclient_connector.generate_insert(
                source_insert_column
            )
            source_update = _source.tapclient_connector.generate_update(
                source_update_column,
                source_where_column
            )
            source_delete = _source.tapclient_connector.generate_delete(
                source_where_column
            )

            # stats check
            _stats = operate.get('stats_check').get('initial+cdc')
            _stats_initial = _stats.get('initial')
            _stats_insert = _stats.get('insert')
            _stats_update = _stats.get('update')
            _stats_delete = _stats.get('delete')
            # target drop table
            self.drop_target_table(_target)
            with allure.step('执行前删除要插入的数据'):
                _source.tapclient_connector.exec(source_delete)
            with allure.step('创建任务'):
                self.create_data_copy_development(data)
            with allure.step('等待任务运行'):
                self.scheduled_task_wait_it_running(task_name)
            source_connection_name = source_info.get('name')
            with allure.step('insert step'):
                self.func_wait(
                    self.get_task_stats_insert,
                    self.get_task_status,
                    task_name,
                    _stats_initial
                )
                self.check_logminer_status(
                    source_connection_name,
                    task_name
                )
                self.tap_check(
                    source_info,
                    source_table,
                    target_info,
                    target_table
                )

            with allure.step('insert step'):
                logger.info('cdc insert and check')
                _source.tapclient_connector.exec(source_insert)
                self.func_wait(
                    self.get_task_stats_insert,
                    self.get_task_status,
                    task_name,
                    _stats_insert
                )
                self.check_logminer_status(
                    source_connection_name,
                    task_name
                )
                self.tap_check(
                    source_info,
                    source_table,
                    target_info,
                    target_table
                )
            with allure.step('update step'):
                logger.info('cdc update and check')
                _source.tapclient_connector.exec(source_update)
                self.func_wait(
                    self.get_task_stats_update,
                    self.get_task_status,
                    task_name,
                    _stats_update
                )
                self.check_logminer_status(
                    source_connection_name,
                    task_name
                )
                self.tap_check(
                    source_info,
                    source_table,
                    target_info,
                    target_table
                )
            with allure.step('delete step'):
                logger.info('cdc delete and check')
                _source.tapclient_connector.exec(source_delete)
                self.func_wait(
                    self.get_task_stats_delete,
                    self.get_task_status,
                    task_name,
                    _stats_delete
                )
                self.check_logminer_status(
                    source_connection_name,
                    task_name
                )
                self.tap_check(
                    source_info,
                    source_table,
                    target_info,
                    target_table
                )
        except Exception as e:
            self.release_table(record_id)
            logger.info(e)
            _source.tapclient_connector.exec(source_delete)
            assert False
        finally:
            self.release_table(record_id)
            if self.task_is_running(task_name):
                self.stop_task_wait_it_paused(task_name)
            self.generate_task_url(task_name, data.get('task_type'))
            try:
                _source.tapclient_connector.close()
                _target.tapclient_connector.close()
            except Exception as e:
                pass
        # 3. 使用这个数据源，创建全量+增量，复制任务|数据开发任务, 任务开启cdc

        # 4. 共享挖掘任务列表出现所属数据源的共享挖掘任务，状态正常

        # 5. 从此共享挖掘任务中，获取到新创建的调度任务信息

        # 6. 共享挖掘中间库挖掘日志检查
